<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">修改密码</div>
            <el-form :model="param" :rules="rules" ref="update" label-width="0px" class="ms-content">
                <el-form-item prop="pwd">
                    <el-input
                        type="password"
                        placeholder="输入要修改的密码"
                        v-model="param.pwd"
                        show-password
                    >
                    </el-input>
                </el-form-item>
                <el-form-item prop="rawpassword">
                    <el-input
                        type="password"
                        placeholder="请再次输入密码"
                        v-model="param.rawpwd"
                        show-password
                    >
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">确定修改</el-button>
                </div>
<!--                <p class="login-tips">Tips : 用户名和密码随便填。</p>-->
            </el-form>
        </div>
    </div>
</template>

<script>
import request from '@/utils/request';

export default {
    created(){
      this.param.username = this.$route.query.username;
    },
    data: function() {
        var validatePass2 = (rule, value, callback) => {
        if (this.param.rawpwd === '') {
          callback(new Error('请再次输入密码'));
        } else if (this.param.rawpwd !== this.param.pwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return {
            param: {
                username:'',
                pwd: '',
                rawpwd:''
            },
            rules: {
                pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                rawpassword:[{ validator: validatePass2, trigger: 'blur' }]
            },
        };
    },
    methods: {
        submitForm() {
            this.$refs.update.validate(valid => {
                if (valid) {
                    let form = new FormData(); 
                    for(const key in this.param){
                        form.append(key,this.param[key])
                    }
                    request.post("http://localhost:8081/Login/updatePwdByUsername",form).then((res) => {
                        if(res.code === 200){
                            this.$message.success('修改成功');
                            this.$router.push('/login');
                        }else{
                          this.$message.error('修改失败');
                        }
                    })

                } 
            });
        },
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/1.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
